//
//  TouchView.m
//  Places
//
//  Created by alex on 9/9/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TouchView.h"
#import "TouchViewDelegate.h"


@implementation TouchView

#pragma mark 
#pragma mark properties

@dynamic isShowing;

- (BOOL)isShowing {
	return self.superview != nil ? YES : NO;
}

#pragma mark 
#pragma mark initialization and dealloc

- (TouchView *)initWithDelegate:(id <TouchViewDelegate>)delegateObj {
	self = [super initWithFrame:CGRectMake(0, 0, 320, 480)];
	delegate = [delegateObj retain];
	return self;
}

- (void)dealloc {
	[delegate release];
	[super dealloc];
}

#pragma mark 
#pragma mark show hide touch view

- (void)hide {
	if (!self.isShowing) {
		return;
	}
	
	[self removeFromSuperview];
}

#pragma mark 
#pragma mark intercepting user taps

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	//NSLog(@"touchesBegan");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	//NSLog(@"touchesMoved");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	// is delegate conforms to protocol
	if ([delegate conformsToProtocol:@protocol(TouchViewDelegate)]) {
		NSArray *touchesArray = [[event touchesForView:self] allObjects];
		UITouch *lastTouch = [touchesArray objectAtIndex:touchesArray.count-1];
		// invoke delegate's method
		[delegate addAnnotationAt:[lastTouch locationInView:self]];
	}
	
	//NSLog(@"touchesEnded");

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	//NSLog(@"touchesCancelled");
}

@end
